import {createRouter, createWebHistory} from 'vue-router'

import {ElMessage} from "element-plus";
// import {useRouterStore} from "@/stores/router";
// import Pinia from "@/stores/pinia";
// import {storeToRefs} from 'pinia'

// /*const myUseRouterStore = useRouterStore(Pinia)
// const {routerData} = storeToRefs(myUseRouterStore)
// console.log("-----------------------------")
// console.log(routerData)*/


let oldRouter = [
    {
        path: '/',
        redirect: "/login"
    },
    {
        path: '/login', component: import('@/views/login/login.vue')
    },
    {
        path: '/register', component: import('@/views/register/register.vue'),
    },
    {
        name: 'main',
        path: '/main',
        component: import('@/views/main/main.vue'),
        children: [

            {
                path: '/userList',
                component: import('@/views/system/user/userList.vue'),
            },
            {
                path: '/deptList',
                component: import('@/views/system/dept/deptList.vue'),
            },
            {
                path: '/menuList',
                component: import('@/views/system/menu/menuList.vue'),
            },
            {
                path: '/roleList',
                component: import('@/views/system/role/roleList.vue'),
            },
        ]
    },
]

// if (JSON.parse(window.sessionStorage.getItem("user")) !== null) {
//     const newRouter = JSON.parse(window.localStorage.getItem("router"))
//     newRouter.forEach(item => {
//         oldRouter[3].children.push({
//             path: item.path,
//             component: import("../views" + item.component + ".vue")
//         })
//     })
// }

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: oldRouter
})

//  配置路由守卫u
router.beforeEach((to, from, next) => {


    if (to.path === "/login") {
        next();
        return
    }
    if (window.sessionStorage.getItem("user") == null) {
        ElMessage.error("先登录")
        next("/login")
        return;
    }
    next()
})

/*router.replace(router.currentRoute.value.fullPath)*/
export default router
